/**
 * 
 */
package com.disys.examples;

/**
 * @author Ubaid Raja
 *
 */
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;


public class LdapContextCreation {
	public static void main(String[] args) {
		LdapContextCreation ldapContxCrtn = new LdapContextCreation();
		LdapContext ctx = ldapContxCrtn.getLdapContext();
	}
	public LdapContext getLdapContext(){
		LdapContext ctx = null;
		try{
			Hashtable env = new Hashtable();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"com.sun.jndi.ldap.LdapCtxFactory");
			env.put(Context.SECURITY_AUTHENTICATION, "Simple");
			//it can be <domain\\userid> something that you use for windows login
			//it can also be
			env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
			env.put(Context.SECURITY_CREDENTIALS, "secret");
			//in following property we specify ldap protocol and connection url.
			//generally the port is 10389
			env.put(Context.PROVIDER_URL, "ldap://localhost:10389");
			ctx = new InitialLdapContext(env, null);
			System.out.println("Connection Successful.");
		}catch(NamingException nex){
			System.out.println("LDAP Connection: FAILED");
			nex.printStackTrace();
		}
		return ctx;
	}

}
